//
//  AWServeAppArtcleMessage.m
//  AtWork2
//
//  Created by lucas on 15/6/4.
//  Copyright (c) 2015年 Foreveross. All rights reserved.
//

#import "AWArticleChatMessage.h"

@implementation AWArticleChatMessage

- (NSArray *)articles {
    NSMutableArray *results = [NSMutableArray array];
    for (NSDictionary *article in [self bodyValueForKey:AWMessageKArticles]) {
        [results addObject:[[AWArticle alloc] initWithDictionary:article]];
    }
    return [results sortedArrayUsingComparator:^NSComparisonResult(AWArticle *article1, AWArticle *article2) {
        if(article1.sort < article2.sort) {
            return NSOrderedAscending;
        }
        if (article1.sort > article2.sort) {
            return NSOrderedDescending;
        }
        return NSOrderedSame;
    }];
}


- (NSString *)searchableContent {
    if(self.articles == nil || self.articles.count == 0){
        return @"";
    }
    return ((AWArticle *)self.articles.firstObject).title ?: @"";
}

@end

@interface AWArticle ()

@property (nonatomic,copy,readwrite) NSString *url;

@property (nonatomic, copy, readwrite) NSString *articleId;

@property (nonatomic, copy, readwrite) NSString *title;

@property (nonatomic, copy, readwrite) NSString *author;

@property (nonatomic, copy, readwrite) NSString *summary;

@property (nonatomic, copy, readwrite) NSString *content;

@property (nonatomic, copy, readwrite) NSString *contentSource;

@property (nonatomic, assign, readwrite, getter=hasShowCover) BOOL showCover;

@property (nonatomic, copy, readwrite) NSString *coverMediaId;

@property (nonatomic,copy,readwrite) NSString *coverUrl;

@property (nonatomic, assign, readwrite) UInt64 createTime;

@property (nonatomic, assign, readwrite) NSUInteger sort;

@end

@implementation AWArticle

+ (instancetype)articleWithDictionary:(NSDictionary *)dictonary {
    return [[self alloc] initWithDictionary:dictonary];
}

- (instancetype)initWithDictionary:(NSDictionary *)dictionary {
    if (self = [super init]) {
        self.url = dictionary[AWURL];
        self.articleId = dictionary[AWMessageKId];
        self.title = dictionary[AWMessageKTitle];
        self.author = dictionary[AWMessageKAuthor];
        self.summary = dictionary[AWMessageKSummary];
        self.content = dictionary[AWMessageKContent];
        self.contentSource = dictionary[AWMessageKContentSource];
        self.showCover = [dictionary[AWMessageKShowCover] boolValue];
        self.coverMediaId = dictionary[AWMessageKCoverMediaId];
        self.coverUrl=dictionary[AWCONVERURL];
        self.createTime = [dictionary[AWMessageKCreateTime] unsignedLongLongValue];
        self.sort = [dictionary[AWMessageKSort] intValue];
    }
    return self;
}

@end